<?php

namespace app\admin\controller\product;

use addons\helper\controller\wxapp\auth\Admin;
use app\common\controller\Backend;

/**
 * 商品分类管理
 */
class Order extends Backend
{
  // 当前对象模型
  protected $model = null;

  public function _initialize()
  {
    parent::_initialize();
    $this->orderModel = model('Product.Order');
    $this->expressModel = model('Product.Expressquery');
    $this->addressModel = model('Business.Address');
    $this->businessModel = model('Business.Business');

    $list = $this->orderModel->select();
    $catelist = [];
    foreach ($list as $item) {
      $catelist[$item['id']] = $item['id'];
    }
    $this->assign('catelist', '------');
  }

  // 查看
  public function index()
  {

    if ($this->request->isAjax()) {
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->orderModel
        ->with(['business', 'expressquery'])
        ->where($where)
        ->order($sort, $order)
        ->count();

      $list = $this->orderModel
        ->with(['business', 'expressquery'])
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();


      $result = array("total" => $total, "rows" => $list);
      return json($result);
    }
    return $this->view->fetch();
  }

  // 查看
  public function view($ids = null)
  {
    $row = $this->orderModel->find($ids);
    // return $row;
    if (!$row) {
      $this->error('订单不存在');
    }
    $this->assign('row', $row);
    return $this->view->fetch();
  }

  // 添加
  public function add()
  {
    if ($this->request->isPOST()) {
      $params = $this->request->post("row/a", [], 'trim');
      $result = $this->orderModel->save($params);
      if (!$result) {
        $this->error($this->orderModel->getError());
      }

      $this->success();
    }
    return $this->view->fetch();
  }

  //详情
  public function detail($ids = null)
  {
    $row = $this->orderModel->find($ids);
    //物流名称
    $express = $this->expressModel->where('id', $row['expressid'])->value('name');
    //地址
    $addressInfo = $this->addressModel->with('region')->where('id', $row['businessaddrid'])->find();
    //用户信息
    $business = $this->businessModel->where('id', $row['busid'])->find();
    // return $business['nickname'];


    // 客户信息
    $info = [
      'nickname' => $business['nickname'], //客户
      'mobile' => $business['mobile'], //客户联系方式
      'consignee' => $addressInfo['consignee'], //收货人
      'consigneemobile' => $addressInfo['mobile'], //收货人联系方式
      'address' => $addressInfo['full_region'] . $addressInfo['address'], //收货地址
      'status' => $row['status_array'][$row['status']], //订单状态
      'code' => $row['code'], //订单号
      'amount' => $row['amount'], //金额
      'expressname' => $express, //物流名称
      'expresscode' => $row['expresscode'], //物流订单号
      '发货员' => '',
      'remark' => $row['remark'], //订单备注
      'refundreason' => $row['refundreason'] //退货原因
    ];

    // return json_encode($info);

    // 订单商品列表

    $this->assign('row', $info);
    return $this->view->fetch();
  }


  // 删除
  public function del($ids = null)
  {
    $row = $this->orderModel->all($ids);

    if (!$row) {
      $this->error('订单不存在');
    }

    $result = $this->orderModel->destroy($ids);

    if (!$result) {
      $this->error($this->orderModel->getError());
    }

    $this->success();
  }
}
